package com.moyuaninfo.service;

import com.moyuaninfo.entity.MyyDataOffCharts;
import com.moyuaninfo.entity.MyyWaterQualityData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.moyuaninfo.repository.MyyDataOffChartsRepository;
import com.moyuaninfo.utils.WaterQualityDateUtils;

import java.util.Date;

/**
 * @author 邱祥霸
 * @date 2020-04-08
 */
@Component
public class MyyDataOffChartsManger {

    @Autowired
    private MyyDataOffChartsRepository myyDataOffChartsRepository;

    @Autowired
    private WaterQualityDateUtils waterQualityDateUtils;

    /**
     * 保存水质超标数据
     *
     * @param myyWaterQualityData: 水质数据
     * @return com.example.jpa.jap_demo.entity.MyyDataOffCharts
     *
     * @author 邱祥霸
     * @date 2020-04-08
     */
    public MyyDataOffCharts save(MyyWaterQualityData myyWaterQualityData) {
        // 水质数据未超标
        if (!waterQualityDateUtils.waterQualityOffCarts(myyWaterQualityData.getRiverwayId(), myyWaterQualityData.getType(), myyWaterQualityData.getValue())) {return null;}

        // 保存水质超标数据
        Date currentDate = new Date();
        MyyDataOffCharts myyDataOffCharts = MyyDataOffCharts.builder()
                .createTime(currentDate.getTime())
                .updateTime(currentDate.getTime())
                .dangerType(1)
                .deviceGroupAddressId(myyWaterQualityData.getDeviceGroupAddressId())
                .equipmentWaterId(myyWaterQualityData.getEquipmentWaterId())
                .riverSection(myyWaterQualityData.getRiverSection())
                .riverwayId(myyWaterQualityData.getRiverwayId())
                .status(0)
                .type(myyWaterQualityData.getType())
                .unit(myyWaterQualityData.getUnit())
                .value(myyWaterQualityData.getValue())
                .build();

        return myyDataOffChartsRepository.save(myyDataOffCharts);
    }
}
